package org.jash.report.ui

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.launch
import org.jash.common.adapter.CommonAdapter
import org.jash.common.log.logDebug
import org.jash.common.mvi.BaseActivity
import org.jash.common.mvi.BaseState
import org.jash.report.R
import org.jash.report.BR
import org.jash.report.databinding.ActivityReportBinding
import org.jash.report.databinding.ReportTypeBinding
import org.jash.report.entity.ReportEntity
import org.jash.report.entity.ReportTypeEntity
import org.jash.report.model.ReportIntent
import org.jash.report.model.ReportState
import org.jash.report.model.ReportViewModel

@Route(path ="/report/report")
class ReportActivity : BaseActivity<ActivityReportBinding, ReportViewModel>() {
    val adapter by lazy { CommonAdapter<ReportEntity>({R.layout.item_report to BR.report}) }
    val dialogAdapter by lazy { CommonAdapter<ReportTypeEntity>({R.layout.item_type to BR.type}, mutableListOf<ReportTypeEntity>(
        ReportTypeEntity(1,"1", "日报"),
        ReportTypeEntity(2,"7", "周报"),
        ReportTypeEntity(3,"30", "月报"),
        ReportTypeEntity(4,"90", "季度报表"),
        ReportTypeEntity(5,"业绩", "业绩报表"),
        ReportTypeEntity(6,"营业", "营业报表"),
    )) }
    val dialog:AlertDialog by lazy {
        val typeBinding = ReportTypeBinding.inflate(LayoutInflater.from(this))
        typeBinding.recycler.adapter = dialogAdapter
        MaterialAlertDialogBuilder(this)
            .setView(typeBinding.root)
            .create()
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.recycler.adapter = adapter
        binding.button.setOnClickListener {
            dialog.show()
        }
        lifecycleScope.launch {
            viewModel.intent.send(ReportIntent.SelReport(1, 1))
        }


    }
    fun loaded(sel: ReportState.ReportList) {
        adapter += sel.list
    }
    fun error(error: BaseState.Error) {
        logDebug(error.msg)
        Toast.makeText(this, error.msg, Toast.LENGTH_SHORT).show()
    }

}